{{!
  Copyright (c) HashiCorp, Inc.
  SPDX-License-Identifier: BUSL-1.1
~}}

<section class="section">
  <header class="acl-explainer">
    <p>
      ACL Roles group one or more Policies into higher-level sets of permissions. A user token can have any number of roles or policies.
    </p>
    <div>
      {{#if (can "write role")}}
        <Hds::Button
          @text="Create Role"
          @icon="plus"
          @route="administration.roles.new"
          {{keyboard-shortcut
            pattern=(array "n" "r")
            action=(action this.goToNewRole)
            label="Create Role"
          }}
          data-test-create-role
        />
      {{else}}
        <Hds::Button
          @text="Create Role"
          @icon="plus"
          disabled
          data-test-disabled-create-role
        />
      {{/if}}
    </div>
  </header>

  {{#if this.roles.length}}
    <Hds::Table @caption="A list of roles for this cluster" class="acl-table"
      @model={{this.roles}}
      @columns={{this.columns}}
      @sortBy="name"
    >
      <:body as |B|>
        <B.Tr 
          {{keyboard-shortcut 
            enumerated=true
            action=(action "openRole" B.data)
          }}
          data-test-role-row={{B.data.name}}
        >
          <B.Td data-test-role-name={{B.data.name}}>
            <LinkTo @route="administration.roles.role" @model={{B.data.id}}>{{B.data.name}}</LinkTo></B.Td>
          <B.Td data-test-role-description>{{B.data.description}}</B.Td>
          {{#if (can "list token")}}
            <B.Td>
              <span data-test-role-total-tokens>{{B.data.tokens.length}}</span>
              {{#if (filter-by "isExpired" B.data.tokens)}}
                <span data-test-role-expired-tokens class="number-expired">({{get (filter-by "isExpired" B.data.tokens) "length"}} expired)</span>
              {{/if}}
            </B.Td>
          {{/if}}
          {{#if (can "list policy")}}
            <B.Td data-test-role-policies>
              <div class="tag-group">
                {{#each B.data.policyNames as |policyName|}}
                  {{#let (find-by "name" policyName this.model.policies) as |policy|}}
                    {{#if policy}}
                      <Hds::Tag @color="primary" @text="{{policy.name}}" @route="administration.policies.policy" @model="{{policy.name}}" />
                    {{else}}
                      <Hds::Tag
                        {{hds-tooltip "This policy has been deleted"}}
                        @text="{{policyName}}"
                      />
                    {{/if}}
                  {{/let}}
                {{else}}
                  No Policies
                {{/each}}
              </div>
            </B.Td>
          {{/if}}
          {{#if (can "destroy role")}}
            <B.Td>
              <Hds::Button @text="Delete" @size="small" @color="critical"
                {{on "click" (perform this.deleteRole B.data)}}
                data-test-delete-role
              />
            </B.Td>
          {{/if}}
        </B.Tr>
      </:body>
    </Hds::Table>

  {{else}}
    <div class="empty-message">
      <h3 data-test-empty-role-list-headline class="empty-message-headline">
        No Roles
      </h3>
      <p class="empty-message-body">
        Get started by <LinkTo @route="administration.roles.new">creating a new role</LinkTo>
      </p>
    </div>
  {{/if}}
</section>
